﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using DomesticPestLib;
using System.Collections;

namespace DomesticPestProject
{
    public partial class frmTransaction : System.Web.UI.Page
    {
        DLayer dlayer;
        ArrayList objalst;
        entyTrans entytran;
        //string dte;
        DateTime dte; 

        
        protected void Page_Load(object sender, EventArgs e)
        {
             if (!Page.IsPostBack)
            {
            //btnCommodity.Enabled = false;
            dte = System.DateTime.Today;
            btnTransaction.Enabled = false;
            Panel1.Visible = false;
            BindDetails();
             }
        }

        private void BindDetails()
        {
            entytran = new entyTrans();
            dlayer = new DLayer();
            objalst = new ArrayList();
            objalst = dlayer.selectTrans();
            gvTrans.DataSource = objalst;
            gvTrans.DataBind();
           
        }


        protected void btnInformation_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmInformation.aspx");
        }

        protected void btnMain_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmMain.aspx");
        }

        protected void btnCommodity_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmCommodity.aspx");
        }

        protected void btnPest_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmPest.aspx");
        }

        protected void btnLctn_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmLoc.aspx");
        }

        protected void btnPsDsrbtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("PestDistribution.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < chkPlantparts.Items.Count; i++)
            {

               
                if (chkPlantparts.Items[i].Selected)
                {

                    txtStoreplantparts.Text += chkPlantparts.Items[i].Text + ",";
                
                }

            }

            entytran = new entyTrans();
            
            entytran.TranCd = txttCd.Text;
            entytran.PestCd = ddlPest.Text;
            entytran.HostCd = ddlHost.Text;
            entytran.StateCd = ddlState.Text;
            entytran.TranInfo = lstAddRmvInfo.Text;
            entytran.TranPart = txtStoreplantparts.Text;
            (new DLayer()).SaveTrans(entytran);
            //BindDetails();
            //clear();
            


        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            lstAddRmvInfo.Items.Remove(lstAddRmvInfo.SelectedItem);
        }

        protected void btnaddlist_Click(object sender, EventArgs e)
        {
            lstAddRmvInfo.Items.Add(lstInfo.SelectedItem);
            
        }

        protected void gvTrans_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string tcode = gvTrans.Rows[e.RowIndex].Cells[2].Text;
            (new DLayer()).DeleteTrans(tcode);
            BindDetails();
            
        }

        protected void gvTrans_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvTrans.EditIndex = e.NewEditIndex;
            BindDetails();
        }

        protected void gvTrans_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            entyTrans trans = new entyTrans();

            TextBox txttCd = gvTrans.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox;
            TextBox txtPest = gvTrans.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox;
            TextBox txtHost = gvTrans.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox;
            TextBox txtState = gvTrans.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox;
            TextBox txtInfo = gvTrans.Rows[e.RowIndex].Cells[6].Controls[0] as TextBox;
            TextBox txtStoreplantparts = gvTrans.Rows[e.RowIndex].Cells[7].Controls[0] as TextBox;

            trans.TranCd = txttCd.Text;
            trans.PestCd = txtPest.Text;
            trans.HostCd = txtHost.Text;
            trans.StateCd = txtState.Text;
            trans.TranInfo = txtInfo.Text;
            trans.TranPart = txtStoreplantparts.Text;
            
            DLayer d = new DLayer();
            (new DLayer()).UpdateTrans(trans);
            gvTrans.EditIndex = -1;
            BindDetails();
        }

        protected void gvTrans_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvTrans.EditIndex = -1;
            BindDetails();
        }

        protected void btnListing_Click(object sender, EventArgs e)
        {
            Panel1.Visible =true;

        }

       
    }
}